namespace Spotted.Storage.Tables.Pages
{
    using System;
    using System.Windows.Controls;
    using Microsoft.WindowsAzure.Samples.Phone.Storage;
    using Spotted.Storage.Tables.ViewModels;

    public partial class ListTablesPage
    {
        public ListTablesPage()
        {
            InitializeComponent();
            this.ViewModel = new ListTablesPageViewModel();
        }

        public ListTablesPageViewModel ViewModel
        {
            get { return this.DataContext as ListTablesPageViewModel; }
            set { this.DataContext = value; }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            this.ViewModel.LoadTable();
        }

        private void OnDeleteTable(object sender, EventArgs e)
        {
            var table = ((Button)sender).Tag as TableServiceSchema;
            if ((this.ViewModel != null) && (table != null))
                this.ViewModel.DeleteTable(table);
        }

        private void OnLoadTable(object sender, EventArgs e)
        {
            this.ViewModel.LoadTable();
        }

        private void OnNewTable(object sender, EventArgs e)
        {
            this.ViewModel.NewTable();
        }

        private void OnSampleData(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Storage/Tables/Pages/SampleDataTablePage.xaml", UriKind.Relative));
        }
    }
}